## FocusDialogs - Handle the FocusFrame dialogs
## Copyright (C) January 26, 2005 Erick P. Bodine
## email: erick_bodine@comcast.net
## webaddress

## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# TODO: Change to 'import wx'
# TODO: Add more docstrings
# TODO: Finish the Help dialog
# TODO: Finish the About dialog
# TODO: Get rid of CalenDlg - use DatePickerCtrl instead
# TODO: Research the CompleteTask dialogs needing to be frame, not dialog
# TODO: Insert proper time into date grabbed from DatePickerCtrls (dpc.GetValue() call) in
#       all relevant dialogs
# TODO: The 'NewProject' & 'ChangeProject' dialogs should be changed to 'TextEntryDialogs'

"""
NAME
FocusDialogs

DESCRIPTION

NAME
Erick P Bodine
"""

import os
import sys
import time
import logging
from wxPython.wx import *
import wxPython.lib.wxpTag
from wxPython.html import *
from wxPython.lib.calendar import CalenDlg

__author__ = "$Author: ebodine $"
__date__ = "$Date: 2005-05-03 16:48:28 -0600 (Tue, 03 May 2005) $"
__version__="$Revision: 40 $"

wildcard = "wxfocus file (*.wxf)|*.wxf|All files (*.*)|*.*"


class GenericCalendarDialog:
    def __init__(self, parent):
        _month_to_number = {
            'January':'1',
            'February':'2',
            'March':'3',
            'April':'4',
            'May':'5',
            'June':'6',
            'July':'7',
            'August':'8',
            'September':'9',
            'October':'10',
            'November':'11',
            'December':'12'
        }

        dlg = CalenDlg(parent)
        dlg.Centre()

        if dlg.ShowModal() == wxID_OK:
            try:
                result = dlg.result
            except AttributeError:
                self.new_date = "no date selected"
            else:
                month = _month_to_number.get(str(result[2]))
                self.new_date =  month+'-'+str(result[1])+'-'+str(result[3])

class GenericDialog(wxDialog):
    """ A generic dialog class for entering new project/task/log or
    changing project/task/log
    """
    def __init__(self, parent, title, label_caption, current_name):
        wxDialog.__init__(self, parent, -1, title, pos=wxDefaultPosition,
                            size=wxDefaultSize, style=wxDEFAULT_DIALOG_STYLE)

        CLEAR_ID = wxNewId()
        TEXT_ID = wxNewId()

        self.label = wxStaticText(self, -1, label_caption)
        self.text = wxTextCtrl(self, TEXT_ID, current_name)
        self.horizontal_line = wxStaticLine(self, -1, style=wxLI_HORIZONTAL)
        self.clear_button = wxButton(self, CLEAR_ID, "Clear")
        self.ok_button = wxButton(self, wxID_OK, "OK")
        self.cancel_button = wxButton(self, wxID_CANCEL, "Cancel")

        self.__do_layout()
        self.value = self.ShowModal()

        #--- Event Handlers ---#
        EVT_BUTTON(self, CLEAR_ID, self.__clearText)

    def __do_layout(self):
        """ Layout the GUI elements on the dialog. """
        self.root_sizer = wxBoxSizer(wxVERTICAL)
        self.button_sizer = wxBoxSizer(wxHORIZONTAL)
        self.button_sizer.Add(self.clear_button, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.button_sizer.Add(self.ok_button, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.button_sizer.Add(self.cancel_button, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.root_sizer.Add(self.label, 0, wxGROW|wxEXPAND|wxTOP|wxLEFT, 10)
        self.root_sizer.Add(self.text, 0, wxGROW|wxEXPAND|wxALL, 10)
        self.root_sizer.Add(self.horizontal_line, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.root_sizer.Add(self.button_sizer, 0, wxGROW|wxEXPAND|wxALL|wxALIGN_TOP, 5)

        self.SetSizer(self.root_sizer)
        self.SetAutoLayout(True)
        self.root_sizer.Fit(self)

        self.CenterOnScreen()

    #--- Event Handler Methods ---#
    def __clearText(self, event):
        """ Clear the text control. """
        self.text.Clear()

class GenericDeleteDialog(wxMessageDialog):
    """ A generic delete dialog for deleting individual projects, tasks,
    or logs.
    """
    def __init__(self, parent, caption, title, mode):
        wxMessageDialog.__init__(self, parent, caption, title,
                                    wxOK|wxCANCEL|wxICON_INFORMATION)
        
class GenericSettingsDialog(wxDialog):
    """ A generic settings dialog. """
    def __init__(self, parent, title):
        wxDialog.__init__(self, parent, -1, title, pos=wxDefaultPosition,
                            size=wxDefaultSize, style=wxDEFAULT_DIALOG_STYLE)

        TREE_ID = wxNewId()
        SPLITTER_ID = wxNewId()


        self.splitter = wxSplitter(self, SPLITTER_ID, style=wxNO_3D|wxSP_3D)
        self.splitter.SetMinimumPaneSize(1)

        self.splitter.SplitVertically(self.tree, )
        self.splitter.SetSashPosition(75)

        # Create button/line controls
        self.horizontal_line = wxStaticLine(self, -1, style=wxLI_HORIZONTAL)
        self.ok_button = wxButton(self, wxID_OK, "OK")
        self.cancel_button = wxButton(self, wxID_CANCEL, "Cancel")

        self.__do_layout()

    def __do_layout(self):
        """ Layout the GUI elements on the dialog. """
        self.root_sizer = wxBoxSizer(wxVERTICAL)
        
        self.button_sizer = wxBoxSizer(wxHORIZONTAL)
        self.button_sizer.Add(self.ok_button, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.button_sizer.Add(self.cancel_button, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.root_sizer.Add(self.horizontal_line, 0, wxGROW|wxEXPAND|wxALL, 5)
        self.root_sizer.Add(self.button_sizer, 0, wxGROW|wxEXPAND|wxALL|wxALIGN_TOP, 5)

        self.SetSizer(self.root_sizer)
        self.SetAutoLayout(True)
        self.root_sizer.Fit(self)

        self.CenterOnScreen()

class NewProjectDialog(GenericDialog):
    def __init__(self, parent, title, label):
        GenericDialog.__init__(self, parent, title, label, "")

        if self.value == wxID_OK:
            new_name = self.text.GetLineText(0)
            print "New project name: %s" % new_name

        else:
            print "You pressed Cancel"
            self.Destroy()

        self.Destroy()

class ChangeProjectDialog(GenericDialog):
    def __init__(self, parent, title, label, current_name):
        GenericDialog.__init__(self, parent, title, label, current_name)

class AddTaskDialog(wxDialog):
    def __init__(self, parent):
        wxDialog.__init__(self, parent, -1, "Add a Task - myfocus", pos=wxDefaultPosition,
                            size=(600,600), style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
                            
        self.titleLabel = wxStaticText(self, -1, "Title")
        self.titleTextCtrl = wxTextCtrl(self, -1, "", style=wxTE_LEFT)
        self.commentsLabel = wxStaticText(self, -1, "Comments")
        self.commentsTextCtrl = wxTextCtrl(self, -1, "", style=wxSUNKEN_BORDER|wxTE_MULTILINE)
        self.startDateLabel = wx.StaticText(self, -1, "Start date")
        self.startDatePicker = wxDatePickerCtrl(self, size=(120,-1),style=wxDP_DROPDOWN|wxDP_SHOWCENTURY)
        self.endDateLabel = wx.StaticText(self, -1, "End date")
        self.endDatePicker = wxDatePickerCtrl(self, size=(120,-1),style=wxDP_DROPDOWN|wxDP_SHOWCENTURY)
        self.hline = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        self.okButton = wxButton(self, wxID_OK)
        self.cancelButton = wxButton(self, wxID_CANCEL)
        
        self.__set_initial_properties()
        self.__do_layout()
        
        self.value = self.ShowModal()
        if self.value == wxID_CANCEL:
            self.Destroy()
        
    def __set_initial_properties(self):
        """ Set any initial control properties. """
        pass
        
    def __do_layout(self):
        """ Layout the GUI elements. """
        self.rootSizer = wxBoxSizer(wxVERTICAL)
        
        self.titleSizer = wxBoxSizer(wxHORIZONTAL)
        self.titleSizer.Add(self.titleLabel, 0, wxALIGN_LEFT|wxTOP|wxRIGHT, 15)
        self.titleSizer.Add(self.titleTextCtrl, 1, wxALIGN_LEFT|wxTOP|wxRIGHT, 10)
        
        self.commentsSizer = wxBoxSizer(wxVERTICAL)
        self.commentsSizer.Add(self.commentsLabel, 0, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        self.commentsSizer.Add(self.commentsTextCtrl, 1, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        
        self.startDateSizer = wxBoxSizer(wxHORIZONTAL)
        self.startDateSizer.Add(self.startDateLabel, 0, wxALIGN_BOTTOM|wxALL, 5)
        self.startDateSizer.Add(self.startDatePicker, 0, wxALIGN_BOTTOM|wxALL, 5)
        
        self.endDateSizer = wxBoxSizer(wxHORIZONTAL)
        self.endDateSizer.Add(self.endDateLabel, 0, wxALIGN_BOTTOM|wxALL, 5)
        self.endDateSizer.Add(self.endDatePicker, 0, wxALIGN_BOTTOM|wxALL, 5)
        
        self.buttonSizer = wxStdDialogButtonSizer()
        self.buttonSizer.AddButton(self.okButton)
        self.buttonSizer.AddButton(self.cancelButton)
        self.buttonSizer.Realize()
        
        self.rootSizer.Add(self.titleSizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.commentsSizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.startDateSizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.endDateSizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.hline, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.buttonSizer, 0, wxALIGN_CENTER_VERTICAL|wxGROW|wxALL, 5)
        
        self.SetSizer(self.rootSizer)
        self.SetAutoLayout(True)
        self.rootSizer.Fit(self)
        
class CompleteTaskDialog(wxDialog):
    """ A dialog for completing the selected task. """
    def __init__(self, parent, task_comments):
        self.task_comments = task_comments
        wxDialog.__init__(self, parent, -1, "Complete Task - wxfocus",
                                pos=wxDefaultPosition, size=(600,600), style=wxDEFAULT_DIALOG_STYLE)
#                                style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)

        # Create the dialog controls
        self.log_check = wxCheckBox(self, -1, "Log the completed task", wxDefaultPosition, 
                                    wxSize(150, 20), wxNO_BORDER)
                                    
        self.changeDateLabel = wxStaticText(self, -1, "Date")
        self.changeDatePicker = wxDatePickerCtrl(self, -1, size=(160,-1), 
                                                    style=wxSUNKEN_BORDER|wxTE_MULTILINE)
        
        self.body_label = wxStaticText(self, -1, "Body")
        self.body_text = wxTextCtrl(self, -1, "", style=wxSUNKEN_BORDER|wxTE_MULTILINE)

        self.upper_vertical_line = wxStaticLine(self, -1, style=wxLI_VERTICAL)
        
        self.schedule_check = wxCheckBox(self, -1, "Schedule follow up task",
                                            wxDefaultPosition, wxSize(150, 20), wxNO_BORDER)
                                            
        self.title_label = wxStaticText(self, -1, "Title")
        self.title_text = wxTextCtrl(self, -1, "")
        self.comments_label = wxStaticText(self, -1, "Comments")
        self.comments_text = wxTextCtrl(self, -1, "", style=wxTE_MULTILINE)

        self.start_label = wxStaticText(self, -1, "Start Date")
        self.startDatePicker = wxDatePickerCtrl(self, -1, size=(160, -1), 
                                                    style=wxSUNKEN_BORDER|wxTE_MULTILINE)
        
        self.endDateLabel = wxStaticText(self, -1, "End Date")
        self.endDatePicker = wxDatePickerCtrl(self, -1, size=(160, -1), 
                                                style=wxSUNKEN_BORDER|wxTE_MULTILINE)
        
        self.root_horizontal = wxStaticLine(self, -1, style=wxLI_HORIZONTAL)

        self.continueButton = wxButton(self, wxID_OK, "Continue")
        self.cancelButton = wxButton(self, wxID_CANCEL)

        self.__set_initial_properties()
        self.__do_layout()

        self.CenterOnScreen()
        
        self.value = self.ShowModal()
        if self.value == wxID_CANCEL:
            self.Destroy()

    def __set_initial_properties(self):
        """ Set any initial control properties. """
        self.log_check.SetValue(True)
        self.body_text.WriteText(self.task_comments)

    def __do_layout(self):
        """ Layout the controls on the dialog. """
        self.root_sizer = wxBoxSizer(wxVERTICAL)

        self.upper_sizer = wxBoxSizer(wxHORIZONTAL)
        self.left_upper = wxBoxSizer(wxVERTICAL)
        self.log_info_sizer = wxStaticBoxSizer(wxStaticBox(self, -1, "Log Information"), wxVERTICAL)
        
        self.date_sizer = wxBoxSizer(wxHORIZONTAL)
        self.date_sizer.Add(self.changeDateLabel, 0, wxRIGHT|wxTOP, 5)
        self.date_sizer.Add(self.changeDatePicker, 0, wxRIGHT|wxEXPAND|wxGROW, 5)
        
        self.log_info_sizer.Add(self.date_sizer, 0, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        self.log_info_sizer.Add(self.body_label, 0, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        self.log_info_sizer.Add(self.body_text, 1, wxALIGN_LEFT|wxGROW|wxEXPAND)

        self.left_upper.Add(self.log_check, 0, wxALIGN_LEFT|wxGROW|wxALL|wxEXPAND)
        self.left_upper.Add(self.log_info_sizer, 1, wxALIGN_LEFT|wxGROW|wxALL|wxEXPAND)
        
        self.right_upper = wxBoxSizer(wxVERTICAL)
        
        self.task_info_sizer = wxStaticBoxSizer(wxStaticBox(self, -1, "Task Information"), wxVERTICAL)

        self.title_sizer = wxBoxSizer(wxHORIZONTAL)
        self.title_sizer.Add(self.title_label, 0, wxALIGN_LEFT|wxTOP|wxRIGHT, 15)
        self.title_sizer.Add(self.title_text, 1, wxALIGN_LEFT|wxGROW|wxEXPAND|wxALL, 5)
        
        self.start_sizer = wxBoxSizer(wxHORIZONTAL)
        self.start_sizer.Add(self.start_label, 0, wxRIGHT|wxTOP, 5)
        self.start_sizer.Add(self.startDatePicker, 0, wxRIGHT|wxEXPAND|wxGROW, 5)

        self.end_sizer = wxBoxSizer(wxHORIZONTAL)
        self.end_sizer.Add(self.endDateLabel, 0, wxRIGHT|wxTOP, 5)
        self.end_sizer.Add(self.endDatePicker, 1, wxRIGHT|wxEXPAND|wxGROW, 5)

        self.task_info_sizer.Add(self.title_sizer, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.task_info_sizer.Add(self.comments_label, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.task_info_sizer.Add(self.comments_text, 1, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.task_info_sizer.Add(self.start_sizer, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.task_info_sizer.Add(self.end_sizer, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)


        self.right_upper.Add(self.schedule_check, 0, wxGROW|wxALL|wxEXPAND, 5)
        self.right_upper.Add(self.task_info_sizer, 1, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)

        self.upper_sizer.Add(self.left_upper, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.upper_sizer.Add(self.upper_vertical_line, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.upper_sizer.Add(self.right_upper, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)

        self.root_buttons_sizer = wxBoxSizer(wxHORIZONTAL)
        self.root_buttons_sizer.Add(self.continueButton, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxALL, 5)
        self.root_buttons_sizer.Add(self.cancelButton, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxALL, 5)

        self.root_sizer.Add(self.upper_sizer, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.root_sizer.Add(self.root_horizontal, 0, wxALIGN_LEFT|wxALL|wxGROW|wxEXPAND, 5)
        self.root_sizer.Add(self.root_buttons_sizer, 0, wxALIGN_RIGHT|wxGROW|wxALL|wxEXPAND, 5)

        self.SetSizer(self.root_sizer)
        self.SetAutoLayout(True)
        self.root_sizer.Fit(self)



class AddLogDialog(wxDialog):
    """ The dialog to add a log. """
    def __init__(self, parent):
        wxDialog.__init__(self, parent, -1, "Add a Log - myfocus", pos=wxDefaultPosition,
                            size=(600,600), style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
                            
        self.logDateLabel = wxStaticText(self, -1, "Log Date")
        self.logDatePicker = wxDatePickerCtrl(self, size=(120,-1),style=wxDP_DROPDOWN|wxDP_SHOWCENTURY)
        self.bodyLabel = wxStaticText(self, -1, "Body")
        self.bodyTextCtrl = wxTextCtrl(self, -1, "", style=wxSUNKEN_BORDER|wxTE_MULTILINE)
        self.hline = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        self.okButton = wxButton(self, wxID_OK)
        self.cancelButton = wxButton(self, wxID_CANCEL)
        
        self.__set_initial_properties()
        self.__do_layout()
        
        self.value = self.ShowModal()
        if self.value == wxID_CANCEL:
            self.Destroy()
    
    def __set_initial_properties(self):
        """ Set any initial control properties. """
        pass
                            
    def __do_layout(self):
        """ Layout the GUI elements. """
        self.rootSizer = wxBoxSizer(wxVERTICAL)
        
        self.logDateSizer = wxBoxSizer(wxHORIZONTAL)
        self.logDateSizer.Add(self.logDateLabel, wxALIGN_BOTTOM|wxALL, 5)
        self.logDateSizer.Add(self.logDatePicker, 0, wxALIGN_BOTTOM|wxALL, 5)
        
        self.bodySizer = wxBoxSizer(wxVERTICAL)
        self.bodySizer.Add(self.bodyLabel, 0, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        self.bodySizer.Add(self.bodyTextCtrl, 1, wxALL|wxALIGN_LEFT|wxGROW|wxEXPAND, 5)
        
        self.buttonSizer = wxStdDialogButtonSizer()
        self.buttonSizer.AddButton(self.okButton)
        self.buttonSizer.AddButton(self.cancelButton)
        self.buttonSizer.Realize()
        
        self.rootSizer.Add(self.logDateSizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.bodySizer, 0, wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.hline, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxGROW|wxEXPAND, 5)
        self.rootSizer.Add(self.buttonSizer, 0, wxALIGN_CENTER_VERTICAL|wxGROW|wxALL, 5)
        
        self.SetSizer(self.rootSizer)
        self.SetAutoLayout(True)
        self.rootSizer.Fit(self)
        
class AboutDialog(wxDialog):

    text = '''
<html>
<body bgcolor="light blue">
<center>
<table bgcolor="white" width="90%%" cellspacing="0" cellpadding="0" border="1">
<tr>
    <td align="center">
    <h1> wxfocus </h1>
    Version %s<br>
    Running on Python %s and wxPython %s<br>
    </td>
</tr>
</table>

<p><b>wxfocus</b> is an application that blah, blah, blah
and more blah will be placed here.</p>

<p>
<b>NoteCards</b> is brought to you by <b>Erick P. Bodine</b>
<p>
<font size="-1">Please see the <i>license.txt</i> file for licensing info.
</font>

<p><wxp class="wxButton">
    <param name="label" value="Close">
    <param name="id" value="wxID_OK">
</wxp></p>
</center>
</body>
</html>
'''

    def __init__(self, parent):
        wxDialog.__init__(self, parent, -1, "About wxfocus",
                                                style=wxDEFAULT_DIALOG_STYLE)
        self.html = wxHtmlWindow(self, -1, size=(420, -1))

        python_version = sys.version.split()[0]
        self.html.SetPage(self.text % ("0.1", python_version, wx.__version__))
        btn = self.html.FindWindowById(wxID_OK)
        btn.SetDefault()

        ir = self.html.GetInternalRepresentation()

        self.html.SetSize( (ir.GetWidth()+4, ir.GetHeight()+4) )
        self.SetClientSize(self.html.GetSize())
        self.CentreOnParent(wxBOTH)

        self.ShowModal()
        self.Destroy()

class HelpDialog(wxDialog):
    def __init__(self, parent):
        wxDialog.__init__(self, parent, -1, "Help for wxfocus",
                                pos=wxDefaultPosition, size=wxDefaultSize,
                                style=wxDEFAULT_DIALOG_STYLE)

        self.__do_layout()

        #--- Event Handlers ---#

        #--- Event Handlers end ---#

        self.ShowModal()
        self.Destroy()

    def __do_layout(self):
        pass
